<template>
  <div>
    <a-tabs :activeKey="first_tab" @change="activeKeyChange">
       <a-tab-pane key="1" tab="在租">
         <a-card :bordered="false" :bodyStyle="tstyle">
          <div class="table-page-search-wrapper">
            <a-form layout="inline">
              <a-row :gutter="48">
                <a-col :md="6" :sm="24">
                  <a-form-item label="搜索條件">
                    <a-select  style="width: 100%" v-model="queryParam.searchType">
                      <a-select-option  style="width: 100%" :value="0">全部</a-select-option>
                      <a-select-option  style="width: 100%" :value="1">房產名</a-select-option>
                      <a-select-option  style="width: 100%" :value="2">房間名</a-select-option>
                      <a-select-option  style="width: 100%" :value="3">業主姓名</a-select-option>
                      <a-select-option  style="width: 100%" :value="4">業主手機號</a-select-option>
                    </a-select>
                  </a-form-item>
                </a-col>
                <a-col :md="8" :sm="24">
                  <a-form-item label="搜索值">
                    <a-input v-model="queryParam.searchValue" allow-clear placeholder="请输入搜索值"/>
                  </a-form-item>
                </a-col>
                <a-col :md="8" :sm="24">
                  <span class="table-page-search-submitButtons">
                    <a-button type="primary" @click="$refs.table.refresh(true)">查询</a-button>
                    <a-button style="margin-left: 8px" @click="() => queryParam = { type: 1 }">重置</a-button>
                  </span>
                </a-col>
              </a-row>
            </a-form>
          </div>
        </a-card>
        <a-card :bordered="false">
          <div>
              <a-button type="primary" icon="plus" @click="$refs.addForm.add()">添加業主</a-button>
          </div>
          <s-table
            ref="table"
            size="default"
            :columns="columns"
            :data="loadData"
            :alert="true"
            :rowKey="(record) => record.id"
            :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
          >
            <span slot="action" slot-scope="text, record">
              <a  @click="$refs.editForm.edit(record.id)">详情</a>
              <a-divider type="vertical"/>
              <a-dropdown >
                  <a class="ant-dropdown-link">
                    更多 <a-icon type="down" />
                  </a>
                  <a-menu slot="overlay">
                    <a-menu-item>
                      <a @click="lookBillById(record.id)">查看賬單</a>
                    </a-menu-item>
                    <a-menu-item v-if="record.leaseStatus !== 2">
                      <a @click="$refs.xuzuOwnerForm.edit(record.id)">續租</a>
                    </a-menu-item>
                    <a-menu-item  v-if="record.leaseStatus === 1">
                      <a @click="$refs.tuiOwnerForm.edit(record.id)">退租</a>
                    </a-menu-item>
                    <a-menu-item  v-if="record.leaseStatus !== 2">
                      <a @click="$refs.billPlan.edit(record.id)">賬單計劃</a>
                    </a-menu-item>
                    <!-- <a-menu-item>
                      <a @click="$refs.printBillDetail.edit(record.id)">打印</a>
                    </a-menu-item> -->
                  </a-menu>
                </a-dropdown>
            </span>
          </s-table>
        </a-card>
       </a-tab-pane>
       <a-tab-pane key="2" tab="歷史業主">
          <a-card :bordered="false" :bodyStyle="tstyle">
          <div class="table-page-search-wrapper">
            <a-form layout="inline">
              <a-row :gutter="48">
                 <a-col :md="6" :sm="24">
                  <a-form-item label="搜索條件">
                    <a-select  style="width: 100%" v-model="queryParam2.searchType">
                      <a-select-option  style="width: 100%" :value="0">全部</a-select-option>
                      <a-select-option  style="width: 100%" :value="1">房產名</a-select-option>
                      <a-select-option  style="width: 100%" :value="2">房間名</a-select-option>
                      <a-select-option  style="width: 100%" :value="3">業主姓名</a-select-option>
                      <a-select-option  style="width: 100%" :value="4">業主手機號</a-select-option>
                    </a-select>
                  </a-form-item>
                </a-col>
                <a-col :md="8" :sm="24">
                  <a-form-item label="搜索值">
                    <a-input v-model="queryParam2.searchValue" allow-clear placeholder="请输入搜索值"/>
                  </a-form-item>
                </a-col>
                <a-col :md="8" :sm="24">
                  <span class="table-page-search-submitButtons">
                    <a-button type="primary" @click="$refs.table2.refresh(true)">查询</a-button>
                    <a-button style="margin-left: 8px" @click="() => queryParam2 = { type: 2 }">重置</a-button>
                  </span>
                </a-col>
              </a-row>
            </a-form>
          </div>
        </a-card>
        <a-card :bordered="false">
          <s-table
            ref="table2"
            size="default"
            :columns="columns"
            :data="loadData2"
            :alert="true"
            :rowKey="(record) => record.id"
            :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
          >
            <span slot="action" slot-scope="text, record">
              <a  @click="$refs.editForm.edit(record.id)">详情</a>
              <a-divider type="vertical"/>
              <a @click="lookBillById(record.id)">查看賬單</a>
            </span>
          </s-table>
        </a-card>
       </a-tab-pane>
    </a-tabs>
    <add-form ref="addForm" @ok="handleOk" />
    <edit-form ref="editForm" @ok="handleOk" />
    <xuzu-owner-form ref="xuzuOwnerForm" @ok="handleOk" />
    <tui-owner-form ref="tuiOwnerForm" @ok="handleOk" />
    <bill-plan ref="billPlan"/>
  </div>
</template>
<script>
  import { STable } from '@/components'
  import { ownerLeasePage } from '@/api/modular/main/ownerlease/ownerLeaseManage'
  import addForm from './addForm.vue'
  import editForm from './editForm.vue'
  import xuzuOwnerForm from '../ownerleasebill/xuzuOwnerForm.vue'
  import tuiOwnerForm from '../ownerleasebill/tuiOwnerForm.vue'
  import billPlan from '../ownerleasebill/billPlan.vue'
  export default {
    components: {
      STable,
      addForm,
      editForm,
      xuzuOwnerForm,
      tuiOwnerForm,
      billPlan
    },
    data () {
      return {
        first_tab: '1',
        // 查询参数
        queryParam: {
          type: 1
        },
        queryParam2: {
          type: 2
        },
        // 表头
        columns: [
          {
            title: '業主名稱',
            align: 'center',
            dataIndex: 'name'
          },
          {
            title: '手機號',
            align: 'center',
            dataIndex: 'phone'
          },
          {
            title: '租期开始时间',
            align: 'center',
            dataIndex: 'rentStartDate'
          },
          {
            title: '租期结束时间',
            align: 'center',
            dataIndex: 'rentEndDate'
          },
          {
            title: '交租週期',
            align: 'center',
            dataIndex: 'receiptCycle'
          },
          {
            title: '租金',
            align: 'center',
            dataIndex: 'rent'
          },
          {
            title: '押金',
            align: 'center',
            dataIndex: 'deposit'
          },
          {
            title: '备注',
            align: 'center',
            dataIndex: 'remark'
          }
        ],
        tstyle: { 'padding-bottom': '0px', 'margin-bottom': '10px' },
        // 加载数据方法 必须为 Promise 对象
        loadData: parameter => {
          return ownerLeasePage(Object.assign(parameter, this.queryParam)).then((res) => {
            return res.data
          })
        },
        loadData2: parameter => {
          return ownerLeasePage(Object.assign(parameter, this.queryParam2)).then((res) => {
            return res.data
          })
        },
        selectedRowKeys: [],
        selectedRows: []
      }
    },
    created () {
      this.columns.push({
          title: '操作',
          width: '150px',
          dataIndex: 'action',
          scopedSlots: { customRender: 'action' }
      })
    },
    methods: {
       // 第一步tab点击
      activeKeyChange(key) {
        this.first_tab = key
      },
      lookBillById (id) {
        this.$router.push({ path: '/ownerbillbyid', query: { id: id } })
      },
      handleOk () {
        this.$refs.table.refresh()
        this.$refs.table2.refresh()
      },
      onSelectChange (selectedRowKeys, selectedRows) {
        this.selectedRowKeys = selectedRowKeys
        this.selectedRows = selectedRows
      }
    }
  }
</script>
<style lang="less">
  .table-operator {
    margin-bottom: 18px;
  }
  button {
    margin-right: 8px;
  }
</style>
